from django.shortcuts import render,redirect
from django.http import HttpResponse
import json, os

from django.http import StreamingHttpResponse
import base64

# Create your views here.

def fileman(request):
    objlist = [1, 2, 3]
    return render(request, 'file.html', {'obj': objlist})


def default(request):
    return  redirect('/file')


def download(request):
    the_file_name = request.GET.get('filename').encode('utf-8')
    print("the_file_name===", the_file_name)
    the_file_name = bytes.decode(the_file_name)
    def file_iterator(the_file_name, chunk_size=512):
        with open(the_file_name, 'rb') as f:
            while True:
                c = f.read(chunk_size)
                if c:
                    yield c
                else:
                    break
    response = StreamingHttpResponse(file_iterator(the_file_name))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(os.path.basename(the_file_name))
    return response


def files1(request):
    if request.method == "POST":
        return HttpResponse(json.dumps(execposts(request)))
    return HttpResponse(json.dumps({"message":"access denied"}))

def encodes(info):
    infostr = json.dumps(info)
    jiamistr = base64.urlsafe_b64encode(str.encode(infostr, encoding='utf8'))
    return jiamistr
def decodes(info):
    jiemi = base64.urlsafe_b64decode(info)
    resultlist=json.loads(bytes.decode(jiemi, encoding='utf8'))
    return resultlist
def files(request):
    if request.method == "POST":
        from explorer.utils import easyfiles
        from explorer.utils import fullfiles
        postdata = request.POST
        getdata = request.GET
        print("原始数据post:",postdata)
        print("原始数据get:",getdata)

        actions = getdata.get('action')
        sessiondata=[]
        datapost={}
        dataget={}
        for k in postdata:
            v=postdata.getlist(k)
            print("获取post数据：%s = %s"%(k,v))
            if len(v)==1:
                v = postdata.get(k)
            elif len(v) == 0:
                v = ""
            datapost[k]=v

        for k in getdata:
            v = getdata.getlist(k)
            print("获取get数据：%s = %s"%(k,v))
            if len(v)==1:
                v = getdata.get(k)
            elif len(v)==0:
                v=""
            dataget[k] = v

        print("转换之后的dataget",dataget)
        print("转换之后的datapost",datapost)
        if request.session.get("sessiondata"):
            sessiondata=request.session.get("sessiondata")
        inputs={
            "actions":actions,
            "getdata":dataget,
            "postdata":datapost,
            "sessiondata":sessiondata
        }
        print("==========================加密之前如下：",inputs)
        print("==========================加密之后，用与传值：",encodes(inputs))
        aa=decodes(fullfiles.easys(encodes(inputs)))

        print("==========================解密之后如下：",aa)
        if len(aa["sessiondata"])>0:
            request.session["sessiondata"]=aa["sessiondata"]
        return HttpResponse(json.dumps(aa['dirlist']))
    return HttpResponse(json.dumps({"message":"access denied"}))
